package com.niodata.dt.fs;

import java.io.IOException;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

public class ObsFileSystemBuilder implements FileSystemBuilder {
  @Override
  public FileSystem createFileSystem(String user, Map<String, String> config) throws IOException {
    String endpoint = config.get(ObsFileSystem.FS_OBS_ENDPOINT);
    String bucket = config.get(ObsFileSystem.FS_OBS_BUCKET);
    String ak = config.get(ObsFileSystem.FS_OBS_ACCESS_KEY);
    String sk = config.get(ObsFileSystem.FS_OBS_SECRET_KEY);

    Configuration conf = new Configuration();
    conf.set(ObsFileSystem.FS_OBS_ACCESS_KEY, ak);
    conf.set(ObsFileSystem.FS_OBS_SECRET_KEY, sk);
    conf.set(ObsFileSystem.FS_OBS_ENDPOINT, endpoint);
    conf.set(ObsFileSystem.FS_OBS_BUCKET, bucket);
    conf.set("fs.defaultFS", "obs://" + bucket + "." + endpoint);
    conf.setClass("fs.obs.impl", ObsFileSystem.class, FileSystem.class);
    return FileSystem.get(conf);
  }
}
